//
//  SEProfileAPI.swift
//  SmartEyeForPatient
//
//  Created by qdsg on 2021/4/20.
//

import Moya

enum SEProfileAPI {
    /// 获取用户个人信息
    case userInfo
    /// 修改用户头像
    /// avatar 头像资源id
    case userAvatar(avatar: String)
    /// 修改用户昵称
    case userNick(nickName: String)
    /// 就诊人列表
    case patientList(pageNum: Int = 1, pageSize: Int = 1000)
    /// 设置默认就诊人
    case setDefaultPatient(id: String = "")
    /// 就诊人详情
    case patientDetail(id: String = "")
    /// 新增/编辑就诊人
    case submitPatient(viewModel: SEPatientDetailViewModel, isEdit: Bool)
    /// 我的关注
    case listOfFollowedDoctors(role: Int, pageNum: Int, pageSize: Int)
    /// 关注医生或医院
    /// userId 用户id
    /// targetId 关注目标id，医院id或医生id
    /// role 关注角色 1医院 2医生
    case followedDoctorOrHospital(userId: String, targetId: String, role: String)
    /// 患者健康档案列表
    case patientFileList(naturalPersonId: String, pageNum:Int, pageSize: Int)
    /// 处方订单列表
    /// bisIds 问诊id 查询问诊关联的处方
    case prescriptionOrderList(pageNum: Int, pageSize: Int, bisIds: [String])
    /// 处方订单详情
    case prescriptionOrderDetail(id: String)
    /// 处方订单提交收货信息
    case prescriptionOrderSubmitAddress(id: String, preId: String, name: String, phone: String, provinceId: String, provinceName: String, cityId: String, cityName: String, areaId: String, areaName: String, address: String)
    /// 处方订单取消
    case prescriptionOrderCancel(id: String)
    /// 查询患者绑卡信息
    case patientDetailGetCard(patientId: String, hospitalId: String)
    /// 查询患者绑卡信息 patientDetailGetCard 返回没有绑卡信息，再调这个接口查询
    case patientGetCard(patientId: String, name: String, idCard: String, phoneNo: String, hospitalId: String)
    /// 创建患者就诊卡
    case patientCreateCard(phoneNo: String, idCard: String, name: String, provinceCode: String, cityCode: String, address: String, hospitalId: String, patientId: String)
}

extension SEProfileAPI: TargetType {
    var baseURL: URL {
        return URL(string: SEApiManager.baseUrl)!
    }

    var path: String {
        switch self {
        case .userInfo:
            return "/smarteye/smarteye-base/user/info"
        case .userAvatar:
            return "/smarteye/smarteye-base/user/change/img"
        case .userNick:
            return "/smarteye/smarteye-base/user/change/user"
        case .patientList:
            return "/smarteye/smarteye-base/patient/query/list"
        case .setDefaultPatient:
            return "/smarteye/smarteye-base/patient/set/default"
        case .patientDetail:
            return "/smarteye/smarteye-base/patient/query/detail"
        case .submitPatient(_, let isEdit):
            return isEdit ? "/smarteye/smarteye-base/patient/edit" : "/smarteye/smarteye-base/patient/add"
        case .listOfFollowedDoctors:
            return "/smarteye/smarteye-base/patient/follow/list"
        case .followedDoctorOrHospital:
            return "/smarteye/smarteye-base/patient/follow"
        case .patientFileList:
            return "/ng/newfuture-bizshare/healthRecord/pageList"
        case .prescriptionOrderList:
            return "/smarteye/smarteye-inquiry/prescription/queryPrescriptionDtoPage"
        case .prescriptionOrderDetail:
            return "/smarteye/smarteye-inquiry/prescription/getPrescriptionDto"
        case .prescriptionOrderSubmitAddress:
            return "/smarteye/smarteye-inquiry/logistics/saveLogistics"
        case .prescriptionOrderCancel:
            return ""
        case .patientDetailGetCard:
            return "/smarteye/smarteye-base/patient/query/id/detailH5"
        case .patientGetCard:
            return "/smarteye/smarteye-base/patient/getCard"
        case .patientCreateCard:
            return "/smarteye/smarteye-base/patient/createCard"
        }
    }

    var method: Moya.Method {
        switch self {
        case .patientFileList,
             .prescriptionOrderDetail,
             .patientDetailGetCard,
             .patientGetCard:
            return .get
        default:
            return .post
        }
    }

    var sampleData: Data {
        return "".data(using: .utf8)!
    }

    var task: Task {
        switch self {
        case .patientFileList,
             .prescriptionOrderDetail,
             .patientDetailGetCard,
             .patientGetCard:
            return .requestParameters(parameters: parameters, encoding: URLEncoding.default)
        default:
            return .requestParameters(parameters: parameters, encoding: JSONEncoding.default)
        }
    }

    var headers: [String : String]? {
        return SEApiManager.headers
    }

    private var parameters: [String : Any] {
        switch self {
        case .userInfo:
            return [:]
        case let .userAvatar(avator):
            return ["avatar": avator]
        case let .userNick(nickName):
            return ["nickName": nickName]
        case let .patientList(pageNum, pageSize):
            return ["pageNum": pageNum,
                    "pageSize": pageSize]
        case let .setDefaultPatient(id):
            return ["id": id]
        case let .patientDetail(id):
            return ["id": id]
        case let .submitPatient(viewModel, isEdit):
            return ["id": isEdit ? viewModel.model.id : "",
                    "name": viewModel.model.name,
                    "idNumber": viewModel.model.idNumber,
                    "province": viewModel.model.province,
                    "city": viewModel.model.city,
                    "region": viewModel.model.region,
                    "addr": viewModel.model.addr,
                    "phone": viewModel.model.phone,
                    "code": viewModel.model.msgCode,
                    "relation": viewModel.model.relation,
                    "height": viewModel.model.height,
                    "weight": viewModel.model.weight,
                    "isSmoking": viewModel.model.isSmoking,
                    "isDrinking": viewModel.model.isDrinking,
                    "isAllergy": viewModel.model.isAllergy,
                    "allergyHistory": viewModel.model.isAllergy == 0 ? "" : viewModel.model.allergyHistory,
                    "diagnoseHistory": viewModel.model.diagnoseHistory]
        case let .listOfFollowedDoctors(role, pageNum, pageSize):
            return ["role": role,
                    "pageNum": pageNum,
                    "pageSize": pageSize]
        case let .followedDoctorOrHospital(userId, targetId, role):
            return ["userId": userId,
                    "targetId": targetId,
                    "role": role]
        case let .patientFileList(naturalPersonId, pageNum, pageSize):
            return ["naturalPersonId": naturalPersonId,
                    "pageNum": pageNum,
                    "pageSize": pageSize]
        case let .prescriptionOrderList(pageNum, pageSize, bisIds):
            return ["pageNum": pageNum,
                    "pageSize": pageSize,
                    "source": 2,
                    "bisIds": bisIds]
        case let .prescriptionOrderDetail(id):
            return ["id": id]
        case let .prescriptionOrderSubmitAddress(id, preId, name, phone, provinceId, provinceName, cityId, cityName, areaId, areaName, address):
            return ["id": id,
                    "preId": preId,
                    "receiveName": name,
                    "receiveMobile": phone,
                    "provideId": provinceId,
                    "provideName": provinceName,
                    "cityId": cityId,
                    "cityName": cityName,
                    "areaId": areaId,
                    "areaName": areaName,
                    "receiverAddress": address]
        case let .prescriptionOrderCancel(id):
            return ["id": id]
        case let .patientDetailGetCard(patientId, hospitalId):
            return ["id": patientId,
                    "hospitalId": hospitalId]
        case let .patientGetCard(patientId, name, idCard, phoneNo, hospitalId):
            return ["patientId": patientId,
                    "name": name,
                    "idCard": idCard,
                    "phoneNo": phoneNo,
                    "hospitalId": hospitalId]
        case let .patientCreateCard(phoneNo, idCard, name, provinceCode, cityCode, address, hospitalId, patientId):
            return ["phoneNo": phoneNo,
                    "idCard": idCard,
                    "name": name,
                    "provinceCode": provinceCode,
                    "cityCode": cityCode,
                    "address": address,
                    "hospitalId": hospitalId,
                    "patientId": patientId]
        }
    }

}
